package com._520it.wms.service.impl;

import com._520it.wms.domain.Employee;
import com._520it.wms.domain.SystemMenu;
import com._520it.wms.mapper.SystemMenuMapper;
import com._520it.wms.query.PageResult;
import com._520it.wms.query.QueryObject;
import com._520it.wms.query.SystemMenuQueryObject;
import com._520it.wms.service.ISystemMenuService;
import com._520it.wms.util.UserContext;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by HASEE on 2017/11/14.
 */
@Service
public class SystemMenuServiceImpl implements ISystemMenuService {
    @Autowired
    private SystemMenuMapper systemMenuMapper;

    @Override
    public void saveOrUpdate(SystemMenu systemMenu) {
        if (systemMenu.getId() != null) {
            systemMenuMapper.updateByPrimaryKey(systemMenu);
        } else {
            systemMenuMapper.insert(systemMenu);
        }

    }

    @Override
    public void delete(Long id) {
        //判断数据是否有子菜单
        SystemMenuQueryObject qo = new SystemMenuQueryObject();
        qo.setParentId(id);
        int count = systemMenuMapper.queryForCount(qo);
        if (count > 0) {
            throw new RuntimeException("该数据有子菜单,不能删除");
        }
        //删除自己
        systemMenuMapper.deleteByPrimaryKey(id);
    }

    @Override
    public SystemMenu get(Long id) {
        return systemMenuMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SystemMenu> listAll() {
        return systemMenuMapper.selectAll();
    }

    @Override
    public PageResult query(QueryObject qo) {
        int totalCount = systemMenuMapper.queryForCount(qo);
        if (totalCount == 0) {
            return new PageResult(qo.getCurrentPage(), qo.getPageSize());
        }
        //查询结果集
        List<SystemMenu> list = systemMenuMapper.queryForList(qo);
        return new PageResult(qo.getCurrentPage(), qo.getPageSize(), list, totalCount);
    }

    @Override
    public List<Map<String, Object>> queryParentList(Long parentId) {
        List<Map<String, Object>> list = new ArrayList<>();
        SystemMenu currentMenu = systemMenuMapper.selectByPrimaryKey(parentId);
        while (currentMenu != null) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", currentMenu.getId());
            map.put("name", currentMenu.getName());
            //获取当前对象之后 去获取上级对象
            currentMenu = currentMenu.getParent();
            list.add(map);
        }
        Collections.reverse(list);
        return list;
    }

    @Override
    public List<Map<String, Object>> queryMenusByParentSn(Long parentId) {
        //1根据当前登录的用户进行判断
        Employee currentUser = (Employee) SecurityUtils.getSubject().getPrincipal();

        //判断是否是管理员 若是管理员 直接根据parentId 查询
        if (currentUser.getAdmin()) {
            return systemMenuMapper.queryMenusByParentSn(parentId);
        } else {
            //根据对应的用户查询其拥有的菜单权限
            return systemMenuMapper.queryMenusByParentSnAndEmpld(parentId, currentUser.getId());

        }
    }

    @Override
    public List<SystemMenu> listChildMenus() {
        return systemMenuMapper.listChildMenus();
    }

}